package com.simu.towercrane.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.util.android.view.CCPullLisvtView;
import cc.util.android.view.CCPullLisvtView.CCOnListViewListener;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVRelation;
import com.avos.avoscloud.FindCallback;
import com.cn.adapters.BaseImageAndTextListAdapter;
import com.cn.adapters.BusinessmanAdapter;
import com.cn.adapters.VendorImageAndTextListAdapter;
import com.simu.constant.TableConstant;
import com.simu.constant.TableConstant.Vendor;
import com.simu.towercrane.Constant;
import com.simu.towercrane.R;
import com.simu.towercrane.activity.BusinessActivity;
import com.simu.towercrane.activity.ProductActivity;
import com.simu.towercrane.adapter.ErshouListAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentShangJia extends BaseFragmentTwo{
	View view=null;
	CCPullLisvtView mVendorListView;
	VendorImageAndTextListAdapter mVendordAdapter;
	ArrayList<Map<String, Object>> mVendorListHashMap;
	private AVQuery<AVObject> query;

	 @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
		 if(view==null){
		 		Log.d("fragmentLife", "FragmentTwoonCreateView");
		 		view=inflater.inflate(R.layout.fragment_glasses_shangjia, null);
		 		mVendorListView=(CCPullLisvtView) view.findViewById(R.id.vendor_list);		 	
		 		mVendorListHashMap=new  ArrayList<Map<String,Object>>();
		 		mVendordAdapter=new VendorImageAndTextListAdapter(getActivity(),mVendorListHashMap, R.layout.vendor_list_item,
		 				new String[]{TableConstant.Vendor.vendorName,TableConstant.Vendor.channelType,TableConstant.Vendor.image,TableConstant.Vendor.isCertified} ,
		 				new int[]{R.id.name_vendor,R.id.channel_vendor,R.id.image_vendor,R.id.certified_vendor});
		 		mVendorListView.setAdapter(mVendordAdapter);		 		
		 		query = new AVQuery<AVObject>("Vendor");
		 		mVendorListView.setCCOnListViewListener(new CCOnListViewListener() {							
					@Override
					public void onRefresh(CCPullLisvtView view) {
						// TODO Auto-generated method stub
						query.orderByDescending("createdAt");

						query.findInBackground(new FindCallback<AVObject>() {							
							@Override
							public void done(List<AVObject> vendorList, AVException arg1) {
								// TODO Auto-generated method stub	
								mVendorListHashMap.clear();
								if(vendorList!=null){
								for(AVObject vendor:vendorList){
									final Map<String, Object> vendorMap=new HashMap<String,Object>();					
									List list=vendor.getList("tag");
									String isCertified="false";
									if(list!=null&&list.size()>=1){
										isCertified=list.get(0)+"";
										if(isCertified.equals("certified")){
											isCertified="true";
										}
									}
									vendorMap.put(TableConstant.Vendor.isCertified,isCertified );	
									vendorMap.put(TableConstant.Vendor.vendorName, vendor.getString(TableConstant.Vendor.vendorName));			
									vendorMap.put(TableConstant.Vendor.objectId,vendor.getObjectId());								
									
									AVRelation<AVObject> channelRe=vendor.getRelation(TableConstant.Vendor.channelType);
									AVRelation<AVObject> imageRe=vendor.getRelation(TableConstant.Vendor.image);
									
									getChannelType(channelRe,vendorMap);
									getImage(imageRe,vendorMap);
									mVendorListHashMap.add(vendorMap);
								}	
								mVendordAdapter.notifyDataSetChanged();
							    mVendorListView.stopRefresh();
								}
							}

							private void getImage(AVRelation<AVObject> iamgeRe, final Map<String, Object> vendorMap) {
								// TODO Auto-generated method stub
								AVQuery<AVObject> avQuery=iamgeRe.getQuery();
								avQuery.findInBackground(new FindCallback<AVObject>() {

									@Override
									public void done(List<AVObject> iamgAVS,
											AVException arg1) {
										// TODO Auto-generated method stub
										if(iamgAVS!=null&&iamgAVS.size()>=1 ){
											AVObject imagAvObject=iamgAVS.get(0);
											AVFile imgAvFile=imagAvObject.getAVFile(TableConstant.Vendor.image);
											vendorMap.put(TableConstant.Vendor.image, imgAvFile.getUrl());
										}
										mVendordAdapter.notifyDataSetChanged();
									}
								});
							}

							private void getChannelType(AVRelation<AVObject> channelRe, final Map<String, Object> vendorMap) {
								  AVQuery<AVObject> avQuery=channelRe.getQuery();
								  avQuery.findInBackground(new FindCallback<AVObject>() {

									@Override
									public void done(List<AVObject> channels,
											AVException arg1) {
										// TODO Auto-generated method stub									
										StringBuffer stringBuffer=new StringBuffer();
										if(channels!=null){
										for(AVObject channel :channels){										
											stringBuffer.append(channel.getString(TableConstant.Vendor.channelName)+"/");
										}										
										stringBuffer.replace(stringBuffer.length()-1, stringBuffer.length(), "");										
										vendorMap.put(TableConstant.Vendor.channelType,stringBuffer.toString() );
										mVendordAdapter.notifyDataSetChanged();
										}
									}
								});
							}
						});
					}					
					@Override
					public void onLoadMore(CCPullLisvtView view) {
						// TODO Auto-generated method stub
						mVendorListView.stopLoadMore();
					}
				});
		 		setItemClick();
		 }
		 Tool.destroyParent(view);
		 return view;		 
	 }

	private void setItemClick() {
		// TODO Auto-generated method stub
		mVendorListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				HashMap map=(HashMap) parent.getAdapter().getItem(position);
				String idString=map.get(TableConstant.Vendor.objectId)+"";
				Toast.makeText(getActivity(), idString, 1000).show();
				Intent intent = new Intent();
				intent.setClass((Activity)getActivity(), BusinessActivity.class);
				intent.putExtra("objectID", idString);
				getActivity().startActivity(intent);
				
				
			}
		});
	}

}
